package com.kzd.sdk.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Environment;
import android.view.View;
import android.widget.TextView;

import com.kzd.sdk.base.OwnerActivity;
import com.kzd.sdk.callback.KZD_HttpCallback;
import com.kzd.sdk.callback.KZD_OnClickListener;
import com.kzd.sdk.core.KZD_ApiConstance;
import com.kzd.sdk.core.KZD_ApiManager;
import com.kzd.sdk.entity.KZD_ServiceInfo;
import com.kzd.sdk.ui.dialog.KZD_ServiceCodeDialog;
import com.kzd.sdk.utils.AppUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import static android.Manifest.permission.READ_EXTERNAL_STORAGE;
import static android.Manifest.permission.WRITE_EXTERNAL_STORAGE;

public class KZD_UserServiceActivity extends OwnerActivity {

    public static void open(Context context) {
        Intent intent = new Intent(context, KZD_UserServiceActivity.class);
        context.startActivity(intent);
    }

    private KZD_ServiceInfo mServiceInfo;

    private final KZD_HttpCallback<KZD_ServiceInfo> httpListenerService = new KZD_HttpCallback<KZD_ServiceInfo>() {
        @Override
        public void onSuccess(KZD_ServiceInfo data) {
            mServiceInfo = data;
            fillData(data);
        }
    };

    @Override
    public String getLayoutIdByName() {
        return "kzd_activity_user_service";
    }

    @Override
    public View getLayoutView() {
        return null;
    }

    private TextView tvUserServiceQQ;

    @Override
    public void initView() {
        tvUserServiceQQ = findViewById(AppUtils.getId((this), "tvUserServiceQQ"));

        findViewById(AppUtils.getId((this), "ivBackPressed"))
                .setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onBackPressed();
                    }
                });

        findViewById(AppUtils.getId((this), "ivUserServiceWX"))
                .setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
//                        String wx = tvUserServiceWX.getText().toString();
//                        ClipboardManager cm = (ClipboardManager) KZD_UserServiceActivity.this
//                                .getSystemService(Context.CLIPBOARD_SERVICE);
//                        cm.setPrimaryClip(ClipData.newPlainText(getPackageName(), wx));
//                        toast("微信:" + wx + "\n已复制，去微信搜索！");
                        KZD_ServiceCodeDialog dialog = new KZD_ServiceCodeDialog((KZD_UserServiceActivity.this));
                        dialog.setImage(AppUtils.getDrawableId((KZD_UserServiceActivity.this), "kzd_gzh_qr"));
                        dialog.setOnClickListener(new KZD_OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                requestDangerousPermissions(new String[]{READ_EXTERNAL_STORAGE, WRITE_EXTERNAL_STORAGE}, 100);
                            }
                        });
                        dialog.show();
                    }
                });

        findViewById(AppUtils.getId((this), "flUserServiceQQ"))
                .setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        AppUtils.launchBrowser((KZD_UserServiceActivity.this), KZD_ApiConstance.GT_GAME_QQ);
                    }
                });

        findViewById(AppUtils.getId((this), "flUserServicePhone"))
                .setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        KZD_ServiceCodeDialog dialog = new KZD_ServiceCodeDialog((KZD_UserServiceActivity.this));
                        dialog.setImage(AppUtils.getDrawableId((KZD_UserServiceActivity.this), "kzd_qy_wx"));
                        dialog.setOnClickListener(new KZD_OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                requestDangerousPermissions(new String[]{READ_EXTERNAL_STORAGE, WRITE_EXTERNAL_STORAGE}, 200);
                            }
                        });
                        dialog.show();
                    }
                });

        findViewById(AppUtils.getId((this), "flUserServiceProblem"))
                .setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                    }
                });
    }

    @Override
    public void initData() {
        KZD_ApiManager.httpServiceInfo(httpListenerService);
    }

    @Override
    public boolean handlePermissionResult(int requestCode, boolean granted) {
        if (!granted) {
            toast("请到应用设置中开启存储权限！");
            return false;
        }
        if (requestCode == 100) {
            Bitmap bitmap = BitmapFactory.decodeResource(getResources(), AppUtils.getDrawableId((this), "kzd_gzh_qr"));
            if (saveImageToGallery(this, bitmap, "gtgzh.png")) {
                toast("保存成功");
            } else {
                toast("保存失败");
            }
        } else if (requestCode == 200) {
            Bitmap bitmap = BitmapFactory.decodeResource(getResources(), AppUtils.getDrawableId((this), "kzd_qy_wx"));
            if (saveImageToGallery(this, bitmap, "gtqy.png")) {
                toast("保存成功");
            } else {
                toast("保存失败");
            }
        }
        return super.handlePermissionResult(requestCode, granted);
    }

    private void fillData(KZD_ServiceInfo serviceInfo) {
        if (serviceInfo == null) {
            return;
        }
        tvUserServiceQQ.setText(serviceInfo.getQq());
    }

    public static boolean saveImageToGallery(Context context, Bitmap bmp, String fileName) {
        File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM), fileName);
        try {
            FileOutputStream fos = new FileOutputStream(file);
            //通过io流的方式来压缩保存图片
            boolean isSuccess = bmp.compress(Bitmap.CompressFormat.PNG, 60, fos);
            fos.flush();
            fos.close();
            //保存图片后发送广播通知更新数据库
            context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(file)));
            return isSuccess;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }
}
